<?php

namespace plugin\webman\scissors;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;

// 剪刀石头布公共方法
class Method
{
    // 判断用户是否在房间内
    static function if_in_room($client_id,$uid){
        
        $create = Db::table('wa_scissors_rooms')->where("createuid",$uid)->where("status",1)->get()->map(function ($value) {return (array)$value;})->toArray();
        // 创建者就删除原来的房间
        if($create){
            $del = Db::table("wa_scissors_rooms")->where("createuid",$uid)->delete();
        }
        $join = Db::table('wa_scissors_rooms')->where("joinuid",$uid)->where("status",1)->get()->map(function ($value) {return (array)$value;})->toArray();
        if($join){
            $del = Db::table("wa_scissors_rooms")->where("createuid",$uid)->update(['joinuid'=>'','join_time'=>0]);
        }
        // if($create || $join){
        //     $res = [
        //         'code'=>1051,
        //         'msg'=>'您已经在房间内，请先退出或解散其他房间',
        //         'info'=>[]
        //     ];
        //     Gateway::sendToClient($client_id,Common::returndata('returncreateroom',$res));
        //     exit();
        // }


    }

    // 创建房间
    static function createroom($client_id,$data){
        $data = [
            'roomno'=> self::createroomno(),
            'createuid' => $data['uid'],
            'createtime' => time(),
            'status' => 1,
            'joinuid' => '',
            'join_time' => 0,
        ];
        $in = Db::table('wa_scissors_rooms')->insert($data);
        if(!$in){
           
            $res = [
                'code'=>1052,
                'msg'=>'房间创建失败',
                'info'=>[]
            ];
            Gateway::sendToClient($client_id,Common::returndata('returncreateroom',$res));
            exit();
        }else{
             // 获取房间内所有人的详细信息
             $data['roomusers'] = self::get_room_users($data['roomno']);
            // 把房主加入 gateway群组
            Gateway::joinGroup($client_id, $data['roomno']);
            return $data;
        }
    }

    // 生成房间号
    static function createroomno(){
        $no = rand(100000,999999);
        return $no;
    }

    // 获取房间内两个人的信息
    static function get_room_users($roomno){
        $roominfo= Db::table("wa_scissors_rooms")->where("roomno",$roomno)->first();
        $data['createuser']= Common::getUserInfo($roominfo->createuid);
        if($roominfo->joinuid !=''){
            
            $data['joinuser']= Common::getUserInfo($roominfo->joinuid);
        }else{
            $data['joinuser']= [];
        }
        $data['roominfo']=$roominfo;
        return $data;
    }

    // 房主关闭房间
    static function creater_closeroom($client_id,$data){
       
       
        // 删除数据库
        $del = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->delete();
        if($del){
            $res = [
                'code'=>1,
                'msg'=>'房主已关闭本房间',
                'info'=>$data['roomno']
            ];
             // 发送群组通知
            Gateway::sendToGroup($data['roomno'],Common::returndata('roomclosed',$res));
            exit();
        }
    }

    // 加入者退出房间
    static function joinr_closeroom($client_id,$data){
        $up = Db::table("wa_scissors_rooms")->where("roomno",$data['roomno'])->update(['joinuid'=>'','join_time'=>0]);
        if($up){
            $res = [
                'code'=>1,
                'msg'=>$data['uid'].'已退出房间',
                'info'=>$data['roomno']
            ];
             // 发送群组通知
            Gateway::sendToGroup($data['roomno'],Common::returndata('userout',$res));
            exit();
        }
    }

    // 判断是否都准备完成
    static function ifallready($roomno){
        $roominfo = Db::table("wa_scissors_rooms")->where("roomno",$roomno)->first();
        if($roominfo->createstatus==1 && $roominfo->joinstatus==1){
            $res = [
                'code'=>1,
                'msg'=>'双方已准备，开始倒计时',
                'info'=>$roominfo
            ];
            Gateway::sendToGroup($roomno,Common::returndata('user_allready',$res));
            return true;
        }else{
            return false;
        }
    }

}